<?php
//------------------------
// 类型属性验证器
//-------------------------

namespace app\common\validate;

use think\Validate;

class TypeAttr extends Validate
{
    protected $rule = [
        'attr_name|属性名称'   => 'require|max:32',
        'seq|排序值'           => 'max:10|number',
        'is_select'            => 'number',
        'opts|可选值'          => 'max:1024',
    ];

    protected $scene = [
        'add'  => ['attr_name'=>'require|max:32|addCheckName:', 'seq','is_select','opts'],
        'edit'  => ['attr_name'=>'require|max:32|editCheckName:', 'seq','is_select','opts'],
    ];

    protected function addCheckName($value){
        $res     = model('TypeAttr')->where(array('attr_name' => $value,'type_id' => input('type_id') ) )->find();
        if($res){
            return '当前类型下已经存在相同名称的属性';
        }
        return true;
    }

    protected function editCheckName($value){
        $TypeAttrModel = model('TypeAttr');

        $old_name = $TypeAttrModel->field('attr_name')->where('id',input('id'))->find();

        if ($value != $old_name['attr_name']) {
            $res = $TypeAttrModel->where(array('attr_name' => $value,'type_id' => input('type_id') ) )->find();
            if($res){
                return '当前类型下已经存在相同名称的属性';
            }
        }

        return true;
    }


}